package cn.thornbird.orgsync.util;

import cn.thornbird.orgsync.model.aliyun.SendSmsVerifyCodeResponseBody;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teaopenapi.models.OpenApiRequest;
import com.aliyun.teaopenapi.models.Params;
import com.aliyun.teautil.models.RuntimeOptions;

import java.util.HashMap;
import java.util.Map;

public class SmsUtil {

    public static SendSmsVerifyCodeResponseBody sendVerificationCode(String accessKeyId, String accessKeySecret,
                                                                     String region, String endpoint,
                                                                     String signName, String templateCode,
                                                                     String mobile, String code)
            throws Exception {
        Map<String, String> map = new HashMap<>();
        map.put("code", code);
        map.put("min", "5");
        String templateParam = JsonUtil.toJsonString(map);
        return sendMobileShortMessage(accessKeyId, accessKeySecret, region, endpoint, signName, templateCode,
                mobile, templateParam);
    }

    public static SendSmsVerifyCodeResponseBody sendMobileShortMessage(String accessKeyId, String accessKeySecret,
                                                                       String region, String endpoint,
                                                                       String signName, String templateCode,
                                                                       String phoneNumber, String templateParam)
            throws Exception {
        Config config = new Config().setCredential(new com.aliyun.credentials.Client());
        config.accessKeyId = accessKeyId;
        config.accessKeySecret = accessKeySecret;
        config.regionId = region;
        config.endpoint = endpoint;
        com.aliyun.teaopenapi.Client client = new com.aliyun.teaopenapi.Client(config);

        Params params = new Params()
                .setAction("SendSmsVerifyCode")
                .setVersion("2017-05-25")
                .setProtocol("HTTPS")
                .setMethod("POST")
                .setAuthType("AK")
                .setStyle("RPC")
                .setPathname("/")
                .setReqBodyType("json")
                .setBodyType("json");

        Map<String, Object> queries = new HashMap<>();
        queries.put("SignName", signName);
        queries.put("TemplateCode", templateCode);
        queries.put("TemplateParam", templateParam);
        queries.put("PhoneNumber", phoneNumber);

        OpenApiRequest request = new OpenApiRequest().setQuery(com.aliyun.openapiutil.Client.query(queries));
        RuntimeOptions runtime = new RuntimeOptions();
        Map<String, ?> result = client.callApi(params, request, runtime);

        SendSmsVerifyCodeResponseBody responseBody = null;
        if (result.containsKey("statusCode") && result.get("statusCode").equals(200)) {
            responseBody = JsonUtil.parseObject(JsonUtil.toJsonString(result.get("body")),
                    SendSmsVerifyCodeResponseBody.class);
        } else {
            int statusCode = result.containsKey("statusCode") ? Integer.valueOf(result.get("statusCode").toString()) : 0;
            throw new Exception(String.format("statusCode: %d", statusCode));
        }
        return responseBody;
    }

}
